function initPlan() {
    $("body").on('click', '.attention_plan_bt li', function () {
        $(this).addClass("current").siblings().removeClass("current");
        var para = $(this).attr('data-type');
        var html = '';
        $.ajax({
            url: 'allPlan/para/' + para,
            type: 'get',
            success: function (res) {
                console.log(res)
//                    html += '<ul class="new_plan_lists">';
                if (res.length > 0) {
                    for (var i = 0; i < res.length; i++) {
                        html += '<li><a href="show/id/' + res[i].plan_id + '"><span class="plan_nr">' + (i + 1) + '.' + res[i].name + '</span><span class="plan_att_num">50%</span></a></li>';
                    }
                } else {
                    html += '<li><a href="#"><span class="plan_nr">暂无数据</span></a></li></ul>'
                }
//                    html += '</ul>'; 
                $('.new_plan_lists').html(html);
            }
        })
    });
    $("body").on('click', '.idTabs li', function () {
        $(this).addClass("allday").siblings().removeClass("allday");
        var dept_para = $(this).attr('data-id');
        var html = '';
        var htmlHeader = '';
        if (!$.isNumeric(dept_para)) {
            history.go(0);
        } else {
            $.ajax({
                url: 'deptPlan/para/' + dept_para,
                type: 'get',
                success: function (res) {
                    console.log(res)
//                        html += '<ul class="new_plan_lists">';
                    if (res.weekPlan.length > 0) {
                        for (var i = 0; i < res.weekPlan.length; i++) {
                            html += '<li><a href="show/id/' + res.weekPlan[i].plan_id + '"><span class="plan_nr">' + (i + 1) + '.' + res.weekPlan[i].name + '</span><span class="plan_att_num">50%</span></a></li>';
                        }
                    } else {
                        html += '<li><a href="#"><span class="plan_nr">暂无数据</span></a></li></ul>'
                    }
//                        html += '</ul>'; 
                    $('.new_plan_lists').html(html);

                    htmlHeader += '<li class="current" data-type="1"><a class="pointer"><span>' + res.count.weekCount + '</span><br/>周计划</a></li><li data-type="2"><a class="pointer"><span>' + res.count.monthCount + '</span><br/>月计划</a></li><li data-type="11"><a class="pointer"><span>' + res.count.projectCount + '</span><br/>项目计划</a></li><li data-type="7"><a class="pointer"><span>' + res.count.yearCount + '</span><br/>年计划</a></li>';
                    $('.attention_plan_bt').html(htmlHeader);
                }
            })
        }
    })
}

function initAddPlan() {
    //判断计划类型
    $('select[name=type]').on('change', function () {
        if ($(this).val() != 1) {
            $('.complex').show();
            $('.simple').hide();
        } else {
            $('.complex').hide();
            $('.simple').show();
        }
    })
    
    //展开子分类
    $('.open').on('click', function () {
        if ($('.childCate').css('display') == 'none') {
            $('.childCate').show();
            $(this).children('label').text('收起子分类');
            $(this).children('img').attr('src', '/Public/Admin/images/arrow-up.png')
        } else {
            $('.childCate').hide();
            $(this).children('label').text('展开子分类');
            $(this).children('img').attr('src', '/Public/Admin/images/arrow-down.png')
        }
    })

    $('.input_lable').on('click', function () {
        $(this).children('i').addClass('radio_bg_check');
        $(this).siblings().children('i').removeClass('radio_bg_check');
        $(this).children().find('input').attr('checked', true);
        $(this).siblings().children().find('input').attr('checked', false);
    })

    //选择人员
    $('.addUser').on('click', function () {
        var id = $(this).attr('data-id');
        var key = $(this).attr('data-key');
        layer.open({
            type: 2,
            title: [
                "选择人员",
                'background-color: #437dba;color:#fff;text-align:center;font-size:1.4em',
            ],
            area: ['80%', '80%'], //宽高
            offset: ['20px', '10%'], //弹出位置 [top,left]
            content: "/index.php/Index/Index/layer/type/1/id/" + id + "/key/" + key
        })
    })
    //选择角色
    $('.addPriv').on('click', function () {
        var id = $(this).attr('data-id');
        var key = $(this).attr('data-key');
        layer.open({
            type: 2,
            title: [
                "选择角色",
                'background-color: #437dba;color:#fff;text-align:center;font-size:1.4em',
            ],
            area: ['500px', '55px'], //宽高
            offset: ['20px', '10%'], //弹出位置 [top,left]
            content: "/index.php/Index/Index/layer/type/7/id/" + id + "/key/" + key
        })
    })
    //清空人员或部门
    $('.delData').on('click', function () {
        $(this).parent('.value').find('.textarea span:not(:first)').remove();
    })

    //删除单个用户
    $('body').on('click', '.delOperator', function () {
        $(this).parent('span').remove();
    })

    //选择部门
    $('.addDept').on('click', function () {
        layer.open({
            type: 2,
            title: [
                "选择部门",
                'background-color: #437dba;color:#fff;text-align:center;font-size:1.4em',
            ],
            area: ['80%', '60%'], //宽高
            offset: ['10%', '10%'], //弹出位置 [top,left]
            content: "/index.php/Index/Index/layer/type/3"
        })
    })
    $(function () {
        var url = $('.getChildType').val() + '?id=' + $('select[name=type]').val();
        getChildType(url);
    })

    $('select[name=type]').on('change', function () {
        var id = $(this).val();
        var url = $('.getChildType').val() + '?id=' + id;
        getChildType(url);
    })

    function getChildType(url) {
        var type = $('.currType').val();
        $.get(url, function (res) {
            console.log(res);
            var html = '<option value="0">请选择</option>';
            if (res) {
                for (var i = 0; i < res.length; i++) {
                    html += '<option value="' + res[i].type_id + '"';
                    if (type == res[i].type_id) {
                        html += 'selected';
                    }
                    html += '>' + res[i].type_name + '</option>';
                }
            } else {
                html += '<option value="">无</option>';
            }
            $('select[name=child_type]').html(html);
        })
    }
}

//进度日志
function initSchedule() {
    $('.is_diary').on('click', function () {
        if ($(this).attr('src') == '/Public/Index/img/s2.png') {
            $(this).attr('src', '/Public/Index/img/s1.png').siblings().attr('src', '/Public/Index/img/s2.png');
            if ($(this).index() == 0) {
                $('input[name=is_diary]').val(1);
            } else {
                $('input[name=is_diary]').val(0);
            }
        } else {
            $(this).attr('src', '/Public/Index/img/s2.png');
            if ($(this).index() == 0) {
                $('input[name=is_diary]').val(0);
            }
        }
    })

    //批注弹窗
    $('body').on('click', '.comment', function () {
        var id = $(this).attr('data-id');
        var is_end = $(this).attr('data-type');
        var dept = $(this).attr('data-dept');
        var cType = $(this).attr('data-cType');
        layer.open({
            type: 2,
            title: [
                "批注内容",
                'background-color: #437dba;color:#fff;text-align:center;font-size:1.4em',
            ],
            area: ['80%', '52%'], //宽高
            offset: ['10%', '10%'], //弹出位置 [top,left]
            content: "/index.php/Index/Index/layer/type/5/is_end/" + is_end + "/id/" + id + "/dept/" + dept + "/cType/" + cType
        })
    })

    //计划查询
    $('.search').on('click', function () {
        var url = $(this).attr('data-url');
        var data = $('form').serialize();
        $.post(url, data, function (res) {
            console.log(res);
            var html = '<table><tr style="text-align:left"><th>计划名称</th><th>完成率</th><th>操作</th></tr>';
            if (res.lists.length > 0) {
                for (var i = 0; i < res.lists.length; i++) {
                    html += '<tr><td width="60%" align="left"><a href="/index.php/Index/Plan/show/id/' + res.lists[i].plan_id + '">' + res.lists[i].name + '</a></td>';
                    html += '<td>'+res.lists[i].total_percent+'%</td>';
//                    html += '<td>' + res.lists[i].status + '</a></td>';
                    html += '<td>';
                    if (res.lists[i].sms_flag == 0) {
                        html += '<a href="/index.php/Index/Plan/schedule/id/' + res.lists[i].plan_id + '">写进度</a><br/>';
                    }
                    if (res.lists[i].publish == 0) {
                        html += '<a class="pointer">提交</a>';
                    } else {
                        html += '<a class="comment pointer" data-id="' + res.lists[i].plan_id + '">批注</a>';
                    }
                    html += '</td></tr>';
                }
                html += '</table>';

            } else {
                html += '<table><tr><td colspan="5" align="center">暂无数据</td></tr></table>'
            }
            $('.transferred_table').html(html);
            $('#total').text(res.total)
        })
    })

    //计划查询输入框重置
    $('.restart').on('click', function () {
        $('.work_cells table input').val('');
        $('.work_cells table select').val(0);
    })

    //增加百分比
    $('.value .add').on('click', function () {
        var num = parseInt($('.p-value').text());
        num += 5;
        if (num > 100) {
            num = 100;
        }
        $('.p-value').text(num);
        $('input[name=percent]').val(num);
    })

    //减少百分比
    $('.value .dec').on('click', function () {
        var oldNum = $('input[name=old_total]').val();
        var num = parseInt($('.p-value').text());
        num -= 5
        if (num < oldNum) {
            num = oldNum;
        }
        $('.p-value').text(num);
        $('input[name=percent]').val(num);
    })

    //删除批注或进度
    $('.delDetail').on('click', function () {
        var url = $(this).attr('data-url');
        layer.confirm('确定删除该条记录吗？', {
            closeBtn: 0,
            btn: ['确定', '取消'] //按钮
        }, function () {
            var index = layer.load(1, {shade: false});
            $.get(url, function (res) {
                console.log(res);
                if (res.status == 1) {
                    layer.msg('删除成功');
                    location.reload();
                } else {
                    layer.msg('删除失败');
                }
            })
        },function(index){
            layer.close(index);
        })
    })
}

function initPlan() {
    $('.myPlan').on('click', function () {
        if ($('.weui_cell_ul').css('display') == 'none') {
            $('.weui_cell_ul').slideDown();
        } else {
            $('.weui_cell_ul').slideUp();
        }
    })
}
